package com.xiguaji.tiktok.v1.tiktok.permission.validate;

import com.xiguaji.tiktok.v1.tiktok.permission.RequestLimit;
import com.xiguaji.tiktok.v1.tiktok.permission.model.Permission;
import com.xiguaji.tiktok.v1.tiktok.permission.repository.PermissionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Optional;


public abstract class Validate {

    public String tips;

    @Autowired
    PermissionRepository permissionRepository;

    protected long permissionId;


    public abstract Boolean validate(long roleId, Object ... params);

    public void setPermissionId(RequestLimit requestLimit){
        if (StringUtils.isEmpty(requestLimit.property())){
            permissionId = permissionRepository.findByName(requestLimit.value()).getId();
            return;
        }
        Optional<Permission> permissionOptional = permissionRepository.findByNameAndProperty(requestLimit.value(),
                requestLimit.property());

        if(permissionOptional.isPresent()){
            permissionId = permissionOptional.get().getId();
        }
    }

    public void setPermissionId(String permissionName){
        permissionId = permissionRepository.findByName(permissionName).getId();
    }


}
